package edu.ptit.jad.preprocess.app;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

import edu.ptit.jad.configuration.CONSTANTS;
import edu.ptit.jad.data.DataAccess;
import edu.ptit.jad.model.Ad;

/**
 * 
 * @author PhuongDo
 * 
 *         AD matrix, is present a matrix with AxD where . A = number of
 *         category D = number of ads A(a,d) = 1 where a category contains ads,
 *         = 0 otherwise This matrix stand for only ads
 */
public class AppGenerateADMatrix {

	int[][] matrix;
	int n;// row
	int m;// column

	public AppGenerateADMatrix(int n, int m) {

		this.n = n;
		this.m = m;

	}

	public void print() {

		for (int i = 0; i < n; i++) {

			for (int j = 0; j < m; j++)
				System.out.print(matrix[i][j] + " ");

			System.out.print("\n");

		}

	}

	public void init() {

		matrix = new int[n][m];

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				matrix[i][j] = 0;

		}
	}

	public void set(int i, int j) {
		this.matrix[i][j] = 1;

	}

	public void save() throws Exception {

		File file = new File(CONSTANTS.FILE_AD_MATRIX);
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(file, false)));

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				out.write(matrix[i][j] + " ");

			out.write("\n");

		}

		out.close();

	}

	public static void main(String[] args) throws Exception {

		// File file = new File(CONSTANTS.FILE_AD_MATRIX);
		// BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
		// new FileOutputStream(file, false)));

		// Load data

		List<String> ads = DataAccess.getInstance().getContents(
				"data/trains/adcat.txt");

		HashMap<String, Integer> cateMap = DataAccess.getInstance()
				.getCatesHashMap();

		int numberOfAds = ads.size();
		int numberOfCates = cateMap.size();

		// System.out.println(numberOfCates);

		// init matrix
		AppGenerateADMatrix matrix = new AppGenerateADMatrix(numberOfCates,
				numberOfAds);
		matrix.init();

		for (String line : ads) {

			String[] row = line.split(":");

			StringTokenizer tknr = new StringTokenizer(row[1].substring(0,
					row[1].length() - 1), ",");
			// System.err.println(row[1].substring(0, row[1].length() - 1));

			int adId = Integer.parseInt(row[0]);

			while (tknr.hasMoreTokens()) {

				// allTermsSet.add(tknr.nextToken());
				String cateName = tknr.nextToken().trim();

				// System.out.println(cateName);

				// int a = cateMap.get(cateName);
				// System.out.println(cateMap.get(cateName) - 1);
				matrix.set(cateMap.get(cateName) - 1, adId - 1);

			}

		}

		// out.close();
		// set value for matrix

		// save matrix
		// matrix.print();

		matrix.save();

	}
}
